package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.DatePicker.DatePicker;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.DueDateBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 22.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 *
 * @desc 设置预产期
 */
public class SetDueDateActivity extends Activity {
    private static final String TAG ="SetDueDateActivity" ;
    @Bind(R.id.activity_set_due_date_rl_iv_return)
    ImageView activitySetDueDateRlIvReturn;
    @Bind(R.id.activity_set_due_date_bt_determine)
    Button activitySetDueDateBtDetermine;
    @Bind(R.id.activity_set_due_date_tv_years)
    TextView activitySetDueDateTvYears;
    @Bind(R.id.activity_set_due_date_rl_Due)
    RelativeLayout activitySetDueDateRlDue;
    @Bind(R.id.activity_set_due_date_rl_calculate_Due)
    RelativeLayout activitySetDueDateRlCalculateDue;


    private Calendar calendar = Calendar.getInstance();
    private  final int mYear = calendar.get(Calendar.YEAR); // 获取当前年份
    private  final int mMonth = calendar.get(Calendar.MONTH) + 1;// 获取当前月份
    private  final int mday = calendar.get(Calendar.DAY_OF_MONTH); // 获取当日

    private SimpleDateFormat simpleDateFormat;

    private double iiiii =  24409589093.0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_due_date);
        ButterKnife.bind(this);

        Utils.setStatusBar(this,false,false);
        Utils.setStatusTextColor(true,this);
        simpleDateFormat = new SimpleDateFormat("y-M-d");

        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String user_set_due_date =  sp.getString("user_set_due_date","");
        if(user_set_due_date.equals("")){

         }else{
            activitySetDueDateTvYears.setText(user_set_due_date);
        }
    }

    @OnClick({R.id.activity_set_due_date_rl_iv_return, R.id.activity_set_due_date_bt_determine, R.id.activity_set_due_date_tv_years, R.id.activity_set_due_date_rl_calculate_Due})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_set_due_date_rl_iv_return:
                Intent intent5 = new Intent(SetDueDateActivity.this, MyStateActivity.class);
                startActivity(intent5);
                finish();
                break;
            case R.id.activity_set_due_date_bt_determine:
                String yeaer = activitySetDueDateTvYears.getText().toString().trim();
                if(!TextUtils.isEmpty(yeaer)){
                    submitDueDate(yeaer);

                }else{
                    Toast.makeText(SetDueDateActivity.this, "请选择预产期", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.activity_set_due_date_tv_years:
                selectRlDue();
                break;
            case R.id.activity_set_due_date_rl_calculate_Due:
                Intent intent = new Intent(SetDueDateActivity.this, CalculateDueActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
    /**
     * @desc 提交预产期
     * @param yeaer*/
    private void submitDueDate(String yeaer) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.CHANGE_DUE_DATE)
                .addParams("cus_id", user_id)
                .addParams("care_date", yeaer)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        final DueDateBean dueDateBean =  gson.fromJson(praiseParseNetworkResponse,DueDateBean.class);
                        if(dueDateBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("user_set_due_date",dueDateBean.data); //预产期
                                    edit.commit();
                                    finish();


                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyApplication.getMcontext(),dueDateBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 选择预产期
     * @desc 将选择好的预产期存放到sharedPrefece中以便使用
     * */
    private void selectRlDue() {
        DatePicker picker = new DatePicker(this);
        picker.setRange(2000, 2030);
        picker.setSelectedItem(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                // showToast(year + "-" + month + "-" + day);

                try {
                    Date date = simpleDateFormat .parse(year + "-" + month + "-" + day);
                    double time = date.getTime();
                    if(time>=System.currentTimeMillis() && time<= (System.currentTimeMillis()+iiiii)){
                        activitySetDueDateTvYears.setText(year + "-" + month + "-" + day);
                    }else{
                        Toast.makeText(SetDueDateActivity.this, "你的选择预产期不在有效时间范围", Toast.LENGTH_SHORT).show();
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
        picker.show();
    }
}
